---! 所有队伍信息
---! key:team_id value:team_ob
local user_teams = {}
local cache_team = {}
local team_max_num = 8 --队伍最大玩家数量
local new_team_id = 1000

--发送邀请状态
local function send_S2CTeamInvite(user_ob, status)
    local result = {}
    result.status = status
    user_ob:send_packet("tanker.S2CTeamInvite", result)
end

local function send_S2CTeamAccept(user_ob, status)
    local result = {}
    result.status = status
    user_ob:send_packet("tanker.S2CTeamAccept", result)
end

local function send_S2CTeamInviteNotify(other, user_ob)
    local result = {}
    result.info = USER_D:get_base_info(user_ob)
    other:send_packet("tanker.S2CTeamInviteNotify", result)
end

local function send_S2CTeamQuitMember(old_team, user_ob)
    local users = old_team:get_users()
    local result = {}
    result.user_id = user_ob:get_id()
    for uid, _ in pairs(users) do
        local other = USER_D:find_user(uid)
        if other then
            other:send_packet("tanker.S2CTeamQuitMember", result)
        end
    end
end

local function send_S2CTeamChangeCaptain(old_team, first_id)
    local users = old_team:get_users()
    local result = {}
    result.user_id = first_id
    for uid, _ in pairs(users) do
        local other = USER_D:find_user(uid)
        if other then
            other:send_packet("tanker.S2CTeamChangeCaptain", result)
        end
    end
end

local function send_S2CTeamChangeCaptain_one(team, user_ob)
    local captain = team:get_captain()
    local result = {}
    result.user_id = captain
    user_ob:send_packet("tanker.S2CTeamChangeCaptain", result)
         
end

---! 发送信息给其他玩家
local function send_S2CTeamNewMember(team, user_ob)
    local users = team:get_users()
    local result = {}
    result.member = USER_D:get_base_info(user_ob)
    for uid, _ in pairs(users) do
        if uid ~= user_ob:get_id() then
            local other = USER_D:find_user(uid)
            if other then
                other:send_packet("tanker.S2CTeamNewMember", result)
            end
        end
    end
end

---! 发送其他成员信息给自己
local function send_S2CTeamAllMember(user_ob, team)
    local users = team:get_users()
    local members = {}
    local join_time = {}
    local user_id = user_ob:get_id()
    for uid, btime in pairs(users) do
        if uid ~= user_id then
            local u_ob = USER_D:find_user(uid)
            if u_ob then
                local info = USER_D:get_base_info(u_ob)
                table.insert(members, info)
                table.insert(join_time, btime)
            end
        end
    end
    local team_info = {}
    team_info.members = members
    team_info.captain = team:get_captain()
    team_info.join_time = join_time

    local result = {}
    result.team = team_info
    user_ob:send_packet("tanker.S2CTeamAllMember", result)
end

----------------------
---组队功能--------
----------------------
TEAM_D = {}

---! 是否在队伍中
local function is_in_team(user_ob)
    if TEAM_D:get_team(user_ob) then
        return true
    end
    return false
end

---! 获取所在队伍
function TEAM_D:get_team(user_ob)
    return user_teams[user_ob:get_team_id()]
end

---! 邀请组队
function TEAM_D:C2STeamInvite(user_ob, msg)
    local other_id = msg.user_id
    local other = USER_D:find_user(other_id)
    --是否离线
    if not other then
        send_S2CTeamInvite(user_ob, 4)
        return
    end
    --对方是否在匹配战斗中
    if other:is_in_room() or other:is_in_combat() then
        send_S2CTeamInvite(user_ob, 3)
        return
    end
    --是否好友
    if not USER_D:check_is_friend(user_ob, other) then
        send_S2CTeamInvite(user_ob, 2)
        return
    end
    --本人是否在匹配战斗中
    if user_ob:is_in_room() or user_ob:is_in_combat() then
        send_S2CTeamInvite(user_ob, 3)
        return
    end

    --队伍是否已满
    local team = self:get_team(user_ob)
    if team then
        if team:get_user_num() > team_max_num then
            send_S2CTeamInvite(user_ob, 5)
            return
        end
        --已经在队伍里
        if team:get_user(other_id) then
            send_S2CTeamInvite(user_ob, 7)
            return
        end
    end
    


    --邀请状态
    send_S2CTeamInvite(user_ob, 1)

    --发出邀请,记录到玩家邀请列表中
    send_S2CTeamInviteNotify(other, user_ob)
    user_ob:team_invite_add_one(other_id)
end

---! 同意邀请
function TEAM_D:C2STeamAccept(user_ob, msg)
    local status = msg.status
    local other_id = msg.user_id
    
    local user_id = user_ob:get_id()
    spdlog.debug("teamd", string.format("C2STeamAccept other_id:%d user_id:%d", other_id, user_id))
    --查在线用户
    
    local other = USER_D:find_user(other_id)
    
    --检查是否在邀请列表中
    if not other or not other:check_team_invite(user_id) then
        spdlog.debug(
            "teamd",
            string.format("other nil or check_team_invite false other_id:%d user_id:%d", other_id, user_id)
        )
        return
    end
    --不同意
    if not status then
        other:team_invite_del_one(user_id)
        return
    end

    if user_ob:get_team_id()~=0 and user_ob:get_team_id() == other:get_team_id() then
        send_S2CTeamAccept(user_ob, 1)
        other:team_invite_del_one(user_id)
        return
    end
    
    --本人是否在匹配战斗中
    if user_ob:is_in_room() or user_ob:is_in_combat() then
        send_S2CTeamAccept(user_ob, 3)
        other:team_invite_del_one(user_id)
        return
    end
    --对方是否在匹配战斗中
    if other:is_in_room() or other:is_in_combat()  then
        send_S2CTeamAccept(user_ob, 3)
        other:team_invite_del_one(user_id)
        return
    end

    --队伍情况
    local team = self:get_team(other)
    if team then
        --已满
        if team:get_user_num() > team_max_num then
            send_S2CTeamAccept(user_ob, 2)
            other:team_invite_del_one(user_id)
            return
        end
        --战斗中
        if team:get_fighting() then
            send_S2CTeamAccept(user_ob, 3)
            other:team_invite_del_one(user_id)
            return
        end
    else
        --没有队伍,则新建队伍
        new_team_id = new_team_id + 1
        team = cache_team[#cache_team]
        if team then
            table.remove(cache_team)
        else
            team = TEAM_OB:create()
        end
        
        team:set_id(new_team_id)
        team:set_captain(other_id)
        team:set_users({other_id}) --增加成员
        user_teams[new_team_id] = team

        other:set_team_id(new_team_id)
    end

    --如果之前已经有队伍,退出,并发送退出信息
    self:quit_team(user_ob)

    --增加成员
    team:set_users({user_id})
    user_ob:set_team_id(team:get_id())

    --发送信息给其他玩家
    send_S2CTeamNewMember(team, user_ob)

    --同意邀请状态
    send_S2CTeamAccept(user_ob, 1)
    --发给自己所有玩家数据
    send_S2CTeamAllMember(user_ob, team)

    if team:get_user_num() == 2 then
        send_S2CTeamChangeCaptain_one(team, user_ob)
        send_S2CTeamChangeCaptain_one(team, other)
    else
        send_S2CTeamChangeCaptain_one(team, user_ob)
    end
    

    --从邀请列表删除
    other:team_invite_del_one(user_id)
    return
end

---! 主动退出队伍
function TEAM_D:C2STeamQuit(user_ob)
    self:quit_team(user_ob)
end

---! 用户离线,则退出队伍
function TEAM_D:quit_team(user_ob)
    ---! 是否已经在队列中
    local old_team = TEAM_D:get_team(user_ob)
    if old_team then
        --发送退出信息 包括自己
        send_S2CTeamQuitMember(old_team, user_ob)

        old_team:del_user(user_ob:get_id())
        --如果是队长则转让
        if old_team:get_captain() == user_ob:get_id() then
            local first_id = old_team:get_first_join()
            if first_id then
                old_team:set_captain(first_id)
                send_S2CTeamChangeCaptain(old_team, first_id)
            end            
        end

        user_ob:set_team_id(0)

        self:check_team_num(old_team)
        
    end
end

---! 解散队伍
function TEAM_D:dismiss_team(team)
    if not team or team:get_id() == 0 then
        return
    end
    local users =  team:get_users() 
    for _, user_ob in ipairs(users) do
        user_ob:set_team_id(0)
    end
    team:clear()
    table.insert(cache_team, team)
end

---! 如果队伍人数为1 则清空
function TEAM_D:check_team_num(old_team)
    if not old_team then
        return
    end
    local ids =  old_team:get_user_ids() 
    if ids and #ids > 1 then
        return
    end
    if ids and #ids == 1 then
        for _, id in ipairs(ids) do
            local user_ob = USER_D:find_user(id)
            if user_ob  then
                user_ob:set_team_id(0)
            end
        end
    end
    old_team:clear()
    table.insert(cache_team, old_team)
end

